<template>
  <div class="app-container">
    <el-table
      :data="wallpaper"
      border
      class="el-table-style"
      style="width: 100%"
    >
      <el-table-column fixed prop="_id" label="编号" width="150"></el-table-column>
      <el-table-column prop="classifyName" label="壁纸分类名称" width="100"></el-table-column>
      <el-table-column prop="nickname" label="发布者昵称" width="100"></el-table-column>
      <el-table-column prop="score" label="评分" width="70"></el-table-column>
      <el-table-column prop="smallPicurl" label="缩略图" width="150">
        <!-- 通过作用域插槽实现子组件向父组件传值 -->
        <template v-slot="scope">
          <el-image style="width: 100%;" :src="scope.row.smallPicurl"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="tabs" label="标签组" width="110" class="el-table-column-tabs">
        <template v-slot="scope">
          <el-tag v-for="(tab,index) in scope.row.tabs" :key="index" class="tabs">{{ tab }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="壁纸描述" width="230">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="170"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="170"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template v-slot="scope">
          <el-button type="primary" size="small" @click="editeWallPaper(scope.row)">编辑</el-button>
          <el-button @click="deleteWallPaper(scope.row)" type="danger" icon="el-icon-delete" size="small">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="paging-bar">
      <span class="demonstration">总数:{{ total }}</span>
      <el-pagination
        class="page-pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { deleteWallPaper, listWallPaper } from '@/api/wallpaper'
import { Message } from 'element-ui'

export default {
  data() {
    return {
      wallpaper: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },

  mounted() {
    this.pageSelect()
  },
  methods: {
    deleteWallPaper(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWallPaper(row._id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          //重新查询
          this.pageSelect()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.pageSelect()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.pageSelect()
    },
    pageSelect() {
      listWallPaper(this.pageSize, this.currentPage, -1).then(res => {
        console.log(res)
        this.wallpaper = res.data.records
        this.total = res.data.total - 0
      }).catch(error => {
        console.log(error)
        Message.error(error)
      })
    },
    editeWallPaper(wallpaper) {
      this.$router.push({
        path: '/wallpaper/publish/edit', query: {
          wallpaper
        }
      })
      this.dialogFormVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>

.app-container {
  width: 100%;
  margin-top: 20px;
  margin-left: 40px;

  .el-table-style {
    margin-top: 30px;
    font-size: 16px;
    .tabs {
      font-size: 15px;
    }
  }

  .paging-bar {
    margin-top: 20px;

    .demonstration {
      font-size: 15px;
      color: #545c64;
    }

    .page-pagination {
      display: inline-block;
      vertical-align: middle;
    }

  }

}

.app-container span {
  font-size: 30px;
}

</style>
